package com.blockmeta.bbs.minemodule.adapter;

import android.content.Context;

import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.utils.TimeUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.util.RouterActivityHelper;
import com.blockmeta.bbs.businesslibrary.widget.CircleImageView;
import com.blockmeta.bbs.minemodule.R;
import com.blockmeta.bbs.minemodule.pojo.ReplyPOJO;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * Created by AirThor
 * Date 2018/4/3.
 */

public class ReplyMsgAdapter extends BaseAdapter<ReplyPOJO, BaseViewHolder> {
    private Context mContext;

    public ReplyMsgAdapter(int layoutResId, @Nullable List<ReplyPOJO> data, Context context) {
        super(layoutResId, data);
        mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, final ReplyPOJO item) {
        helper.setText(R.id.news_msg_tv_name, item.fromUser);
        helper.setText(R.id.news_msg_tv_time, TimeUtil.getTimeFormatText(item.created));
        TextView tvReply = helper.getView(R.id.news_msg_tv_reply);

//        helper.setText(R.id.news_msg_tv_reply,item.extra.message);

        String imgUrl = item.extra.avatar;
        CircleImageView imageView = helper.getView(R.id.news_msg_iv_avatar);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RouterActivityHelper.openUserCenter(mContext, item.extra.uid);
            }
        });
        if (!TextUtils.isEmpty(imgUrl)) {
            Glide.with(mContext).load(item.extra.avatar)
                    .dontAnimate()
                    .into(imageView);
        }else {
            imageView.setImageResource(R.drawable.photo_default);
        }
        helper.getView(R.id.news_msg_main).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if (!TextUtils.isEmpty(item.extra.deepLink)) {
//                    DeepLinkUtils.jumpToTarget(view.getContext(), Uri.parse(item.extra.deepLink));
//                }
                jumpToTarget(item);
            }
        });
        if (item.extra == null||item.extra.fromIdtype == null) return;
        if (item.extra.fromIdtype.equals("quote")) {
            //评论实际内容
            if (!TextUtils.isEmpty(item.extra.quote_content)) {
                helper.setText(R.id.news_msg_tv_content, item.extra.quote_content);
            } else {
                helper.setText(R.id.news_msg_tv_content, "");

            }
            if (!TextUtils.isEmpty(item.extra.comment_content)) {
                helper.setText(R.id.news_msg_tv_reply, item.extra.comment_content);
            } else {
                helper.setText(R.id.news_msg_tv_reply, item.extra.comment_content);
            }
        } else {
            if (!TextUtils.isEmpty(item.extra.title)) {
                helper.setText(R.id.news_msg_tv_content, item.extra.title);
            } else {
                helper.setText(R.id.news_msg_tv_content, "");
            }
            if (!TextUtils.isEmpty(item.extra.message)) {
                helper.setText(R.id.news_msg_tv_reply, item.extra.message);
            } else {
                helper.setText(R.id.news_msg_tv_reply, "");
            }
        }

    }

    /**
     * 跳转页面
     *
     * @param item
     */
    private void jumpToTarget(ReplyPOJO item) {
        if (item.extra == null || item.extra.tid == null) return;
        if (item.msgType==2) {
            RouterHelper.buildPath(RouterPath.BBS_DETAIL_ACTIVITY)
                    .withString("tid",item.extra.tid+"")
                    .navigation(mContext);
        }else if (item.msgType==1){
            RouterHelper.buildPath(RouterPath.NEWS_INNER_DETAIL_ACTIVITY)
            .withString(Constant.ONE,item.extra.tid).navigation(mContext);
        }

    }

}
